/**
 *
 * Copyright by Samuel Castro & Joaquim Fonseca, 2011
 * All Rights Reserved
 *
 * NOTES:
 * Please do not redistribute this plugin without my permission
 * Plugin made for private use only
 *
 */
package com.SySammy.SdCurrency.commands;

import com.SySammy.SdCurrency.Handlers.BankHandler;
import com.SySammy.SdCurrency.Handlers.CurrencyHandler;
import com.SySammy.SdCurrency.Handlers.LoanRun;
import com.SySammy.SdCurrency.Handlers.RepeatingTask;
import com.SySammy.SdCurrency.Prop.CurrencyProperties;
import com.SySammy.SdCurrency.sdC;
import java.util.Set;
import java.util.TreeSet;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

public class sdCBankCmds implements CommandExecutor {

    private final sdC plugin;
    private BankHandler BH = new BankHandler();
    private CurrencyHandler CH = new CurrencyHandler();
    public Set<String> loans = new TreeSet<String>();

    public sdCBankCmds(sdC plugin) {
        this.plugin = plugin;
    }

    @Override
    public boolean onCommand(CommandSender cs, Command cmnd, String string, String[] strings) {
        String[] Nargs = strings;
        String cmd = cmnd.getName().toLowerCase();
        if (cmd.equals("bank")) {
            return bankMethod(cs, Nargs);
        }
        return true;
    }

    private boolean bankMethod(CommandSender sender, String[] string) {
        String[] args = string;
        Player ply = (Player) sender;
        if (args.length <= 0) {
            if (!plugin.casUseBankInfo(ply)) {
                ply.sendMessage(ChatColor.RED + "You don't have permisson to use that command");
                return true;
            }
            if (!BH.HasLoan(ply.getName())) {
                ply.sendMessage(ChatColor.RED + "You don't have a bank loan.");
                ply.sendMessage(ChatColor.DARK_AQUA + "Usury: " + ChatColor.GOLD + CurrencyProperties.usury + "%");
                ply.sendMessage(ChatColor.DARK_AQUA + "Try: /bank help");
                return true;
            }
            ply.sendMessage(ChatColor.DARK_AQUA + "Updaded Bank loan information: ");
            ply.sendMessage(ChatColor.DARK_AQUA + "Amount: " + ChatColor.GOLD + BH.GetloanQnt(ply.getName()) + " " + CurrencyProperties.Currency);
            ply.sendMessage(ChatColor.DARK_AQUA + "Left to repay: " + ChatColor.GOLD + BH.GetloanRepaid(ply.getName()) + " " + CurrencyProperties.Currency);
            ply.sendMessage(ChatColor.DARK_AQUA + "Usury: " + ChatColor.GOLD + CurrencyProperties.usury + "%");
            if (BH.GetloanTimeLeft(ply.getName()) >= 24) {
                ply.sendMessage(ChatColor.DARK_AQUA + "DeadLine: " + ChatColor.GOLD + BH.GetloanTimeLeft(ply.getName()) / 24 + " Day(s)" + ChatColor.RED + " [Updated every: " + CurrencyProperties.ScheduleRepTime + " hour(s)]");
                return true;
            } else {
                ply.sendMessage(ChatColor.DARK_AQUA + "DeadLine: " + ChatColor.GOLD + BH.GetloanTimeLeft(ply.getName()) + " Hour(s)" + ChatColor.RED + " [Updated every: " + CurrencyProperties.ScheduleRepTime + " hour(s)]");
                return true;
            }
        } else if (args[0].equalsIgnoreCase("-l") && args.length >= 3) {
            try {
                if (!plugin.casUseBankLoan(ply)) {
                    ply.sendMessage(ChatColor.RED + "You don't have permisson to use that command");
                    return true;
                }
                if (BH.HasLoan(ply.getName())) {
                    ply.sendMessage(ChatColor.RED + "You already have a loan on the bank.");
                    return true;
                }
                int amount = Integer.parseInt(args[1]);
                int time = Integer.parseInt(args[2]);

                if (amount <= CurrencyProperties.maxLoanQnt) {
                    if (time <= CurrencyProperties.maxLoanTime) {
                        int Schedule = plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new LoanRun(ply.getName().toLowerCase(), plugin), (time * 86400 * 20L));
                        int ScheduleRep = plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new RepeatingTask(ply.getName(), plugin), CurrencyProperties.ScheduleRepTime * 3600 * 21L, CurrencyProperties.ScheduleRepTime * 3600 * 20L);
                        BH.MakeLoan(ply, amount, time, Schedule, ScheduleRep);
                        CH.GiveMoney(ply.getName(), amount);
                        ply.sendMessage(ChatColor.DARK_AQUA + "You have requested a bank loan: ");
                        ply.sendMessage(ChatColor.DARK_AQUA + "Amount: " + ChatColor.GOLD + BH.GetloanQnt(ply.getName()) + " " + CurrencyProperties.Currency);
                        ply.sendMessage(ChatColor.DARK_AQUA + "Repay: " + ChatColor.GOLD + BH.GetloanToPay(ply.getName()) + " " + CurrencyProperties.Currency);
                        ply.sendMessage(ChatColor.DARK_AQUA + "Usury: " + ChatColor.GOLD + CurrencyProperties.usury + "%");
                        ply.sendMessage(ChatColor.DARK_AQUA + "DeadLine: " + ChatColor.GOLD + BH.GetloanTime(ply.getName()) + " Day(s)");
                        return true;
                    } else {
                        ply.sendMessage(ChatColor.RED + "Max loan Time : " + ChatColor.GOLD + CurrencyProperties.maxLoanTime + " day(s)");
                        return true;
                    }
                } else {
                    ply.sendMessage(ChatColor.RED + "Max loan Quantity: " + ChatColor.GOLD + String.valueOf(CurrencyProperties.maxLoanQnt) + " " + CH.GetCurrency());
                    return true;
                }
            } catch (Exception e) {
                ply.sendMessage(ChatColor.RED + "Input error, please try again.");
            }
        } else if (args[0].equalsIgnoreCase("-p") && args.length >= 1) {
            if (!plugin.casUseBankRepay(ply)) {
                ply.sendMessage(ChatColor.RED + "You don't have permisson to use that command");
                return true;
            }
            if (!BH.HasLoan(ply.getName())) {
                ply.sendMessage(ChatColor.RED + "You haven't started a loan on the bank.");
                return true;
            }
            int amount = Integer.parseInt(args[1]);
            if (!CH.HasMoney(ply.getName(), amount)) {
                ply.sendMessage(ChatColor.RED + "You don't have that much money.");
                return true;
            }
            if (amount >= BH.GetloanToPay(ply.getName())) {
                ply.sendMessage(ChatColor.RED + "You don't need to repay that much money.");
                return true;
            }
            CH.TakeMoney(ply.getName(), amount);
            BH.RepayLoan(ply, amount);
            if (BH.GetloanRepaid(ply.getName()) <= 0) {
                ply.sendMessage(ChatColor.GREEN + "You have finished repairing your debt, have an honest day.");
                plugin.getServer().getScheduler().cancelTask(BH.GetloanIDRep(ply.getName()));
                plugin.getServer().getScheduler().cancelTask(BH.GetloanID(ply.getName()));
                BH.RemoveLoan(ply.getName());
                return true;
            } else {
                ply.sendMessage(ChatColor.DARK_AQUA + "You repaid " + ChatColor.GOLD + amount + " " + CH.GetCurrency() + ChatColor.DARK_AQUA + ", still to pay: " + ChatColor.GOLD + BH.GetloanRepaid(ply.getName()) + " " + CH.GetCurrency());
                return true;
            }
        } else if (args[0].equalsIgnoreCase("-h")) {
            if (!plugin.casUseBankHelp(ply)) {
                ply.sendMessage(ChatColor.RED + "You don't have permisson to use that command");
                return true;
            }
            return false;
        }
        return true;
    }
}
